package golibs

import (
	"math"
	"math/rand"
	"time"
)

func Is_prime(num int) bool {
	if num < 2 {
		return false
	}
	if num == 2 {
		return true
	}
	end := int(math.Sqrt(float64(num))) + 1
	for i := 2; i < end; i++ {
		if num%i == 0 {
			return false
		}
	}
	return true
}
func Get_Prime(min int, max int) int {
	diff := max - min
	rand.Seed(time.Now().UnixNano())
	n := rand.Intn(diff)
	data := n + min
	for {
		if Is_prime(data) {
			return data
		} else {
			data += 1
		}
	}
}

func ex_gcd(a int, b int) (int, int, int) {
	if b == 0 {
		return 1, 0, a
	}
	x, y, gcd := ex_gcd(b, a%b)

	return y, x - a/b*y, gcd

}
func mod(e int, d int) int {
	if d > e {
		e, d = d, e
	}
	_, y, _ := ex_gcd(e, d)
	if y < 0 {
		return e + y%e
	}
	return y % e
}
func test() {
	a := Get_Prime(1, 100)
	b := Get_Prime(102, 200)
	n := a * b
	println(a, b)
	q := (a - 1) * (b - 1)
	println(q)
	e := Get_Prime(1, q)
	d := mod(e, q)
	println(n, e, d)

}
